<?php
include("_config.php");
class Mlobby extends CI_Model {

    

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function checkAvailability($name)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT ID FROM users WHERE pseudo = :pseudo");
        $req->execute(array(':pseudo' => $name)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        if($result)
        {
            return false;
        }
    
        $req = $PDO->prepare("SELECT ID FROM membres WHERE login = :pseudo");
        $req->execute(array(':pseudo' => $name)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        if($result)
        {
            return false;
        }
        return true;
    }
    
    function getUserInfo($hash)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT users.*, membres.win, membres.lost, membres.facebook_id, membres.cookie, membres.email FROM users JOIN membres ON users.pseudo = membres.login WHERE hash = :hash");
        $req->execute(array(':hash' => $hash)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        return $result;
    }
    
    function findUserWithCookie($cookie)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT * FROM membres WHERE cookie = :c");
        $req->execute(array(':c' => $cookie)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        if($result){
            return $result["login"];
        } else {
            return false;
        }
    }
    
    function setExpired($hash)
    {
        $PDO = connectDatabase();
        $heure = time() - 10;
        $req = $PDO->prepare("UPDATE users SET expire = :heure WHERE hash = :hash");
        $req->execute(array('hash' => $hash, "heure" => $heure)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        return true;
    }

    function getUsersOnline()
    {
        $PDO = connectDatabase();
        $req = $PDO->query("SELECT * FROM users");
        $req = $req->fetchAll();
        if($req)
        { return $req; }
        else { return null; }
    }

    function checkVisitorIsPlayer($hash)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT ID FROM users WHERE hash = :hash");
        $req->execute(array('hash' => $hash)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        if($result)
        {
            $heure = time() + 60;
            $req = $PDO->prepare("UPDATE users SET expire = :heure WHERE hash = :hash");
            $req->execute(array('hash' => $hash, "heure" => $heure)) or die(print_r($req->errorInfo()));
            $req->closeCursor();
            return true;
        } else {
            return false;
        }
    }
    
    function extendExpire($hash)
    {
        $PDO = connectDatabase();
        $heure = time() + 60;
        $req = $PDO->prepare("UPDATE users SET expire = :heure WHERE hash = :hash");
        $req->execute(array('hash' => $hash, "heure" => $heure)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        $req = $PDO->prepare("SELECT * FROM users WHERE hash = :hash");
        $req->execute(array('hash' => $hash)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();    
        if($result){
            $result["now"] = $heure-60;
        }
        return $result;
    }
    
    function logoutUser($hash)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("DELETE FROM users WHERE hash = :hash");
        $req->execute(array('hash' => $hash)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        return true;
    }

    function newPlayerRegistered($login, $password, $hash)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT ID FROM membres WHERE login = :pseudo AND password = :password");
        $req->execute(array(':pseudo' => $login, ':password' => $password)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        if(!$result)
        {
            return false;
        }
        $req = $PDO->prepare("DELETE FROM users WHERE pseudo = :pseudo");
        $req->execute(array(':pseudo' => $login)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        
        $heure = time() + 60;
        $req = $PDO->prepare("INSERT INTO users(pseudo, hash, isPlaying, expire, perso) VALUES (:pseudo, :hash, 0, :temps, 2)");
        $result = $req->execute(array(':pseudo' => $login, ':hash' => $hash, ':temps' => $heure)) or die(print_r($req->errorInfo()));
        $req->closeCursor();

        return $result;
    }
    
    function newPlayerAnonymous($pseudo, $hash)
    {
        $PDO = connectDatabase();

        $heure = time() + 60;
        $req = $PDO->prepare("DELETE FROM users WHERE pseudo = :pseudo");
        $result = $req->execute(array(':pseudo' => $pseudo)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        $req = $PDO->prepare("INSERT INTO users(pseudo, hash, isPlaying, expire, perso) VALUES (:pseudo, :hash, 0, :temps, 2)");
        $result = $req->execute(array(':pseudo' => $pseudo, ':hash' => $hash, ':temps' => $heure)) or die(print_r($req->errorInfo()));
        $req->closeCursor();

        return $result;
    }

    function deleteExpired()
    {
        $PDO = connectDatabase();

        $heure = time();
        $req = $PDO->prepare("DELETE FROM users WHERE expire <= :heure");

        $result = $req->execute(array(':heure' => $heure)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
    }
    
    function changePerso($hash, $perso)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("UPDATE users SET perso = :perso WHERE hash = :hash");
        $req->execute(array('hash' => $hash, "perso" => $perso)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
    }
    
    function isFacebookUserIsRegistered($facebook_id)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT * FROM membres WHERE facebook_id = :fbid");
        $result = $req->execute(array(':fbid' => $facebook_id)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        if(!$result)
        {
            return false;
        }
        else {
            return $result;
        }
    }
    
    function RR()
    {
        $PDO = connectDatabase();
        $random = rand().time();
        $req = $PDO->prepare("SELECT ID FROM membres");
        $req->execute();
        $res = $req->fetchAll();
        $req->closeCursor();
        print_r($res);
        foreach($res as $r)
        {
            $rr = $random.$r["ID"];
            $rr = sha1($rr);
            $res1 = $PDO->prepare("UPDATE membres SET cookie = :c WHERE ID = :id");
            $res1->execute(array("c" => $rr, "id" => $r["ID"]));
        }
        
    }
    
    function registerUser($u)
    {
        $PDO = connectDatabase();
        $random = rand().time();
        $random = sha1($random);
        $req = $PDO->prepare("INSERT INTO membres(login, password, name, email, gender, birthday, win, lost, facebook_id) VALUES 
            (:log, :pwd, :name, :mail, :gender, :birth, 0, 0, :fbid)");
        /* SQL login password name email gender birthday win lost facebook_id  */
        /*            [name] => Romain Beuque
            [pseudo] => Rbeuque74
            [email] => rbeuque@hotmail.com
            [password] => 12345678
            [gender] => male
            [birthday] => 12/27/1992*/
     /*   $birth = explode("/",$u["birthday"]);
        if(strlen($birth[0]) < 2)
            $birth[0] = "0".$birth[0];
        if(strlen($birth[1]) < 2)
            $birth[1] = "0".$birth[1];
        $u["birthday"] = $birth[2].$birth[0].$birth[1];*/
        
        $array = array('log' => $u["pseudo"], 'pwd' => $u["password"], 'name' => $u["name"], 'mail' => $u["email"], 
            'gender' => $u["gender"], 'birth' => $u["birthday"], 'fbid' => $u["facebook_id"]);
        $result = $req->execute($array) or die(print_r($req->errorInfo()));
        $req->closeCursor();

        return $result;
    }

}